package com.sale.plugins;

import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.SpringContextLoader;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 申购统计查询
 *
 * @author admin
 */
public class SaleFindBuyInfoPluginImpl implements CustomMethodPlugin {

    private Logger log = Logger.getLogger(getClass());

    @Override
    public Object beforeInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        String delStatus = (String) param.get("trsBuy.w_delStatus");
        String proNo = (String) (param.get("trsBuy.wl_proNo") == null ?
                "" :
                param.get("trsNumbers.wl_proNo"));
        String proName = (String) (param.get("trsBuy.wl_proName") == null ?
                "" :
                param.get("trsNumbers.wl_proName"));
        StringBuffer dyncSql = new StringBuffer(
                "select sum(t.num) num,t.sale_name,t.pro_no,t.pro_name,t.price from trs_buy t where  t.del_status='F' ");
        if (StrUtil.isNotEmpty(proNo)) {
            dyncSql.append(" and t.pro_no like'%" + proNo + "%'");
        }
        if (StrUtil.isNotEmpty(proName)) {
            dyncSql.append(" and t.pro_name like'%" + proName + "%'");
        }
        dyncSql.append(" group by t.sale_name,t.pro_no,t.pro_name,t.price");
        dyncSql.append(" order by t.pro_no");
        //param.put("plugin_execSql", dyncSql.toString());
        Map<String, String> result = new HashMap<>(1);
        result.put(CustomMethodPlugin.pluginSql, dyncSql.toString());
        return result;
    }

    @Override
    public Object doInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * @return
     */
    @SuppressWarnings({ "unused", "rawtypes", "unchecked" })
    private List<DataMap> getBuyInfo(String dyncSql) {
        ProxyManager netProxyManager = (ProxyManager) SpringContextLoader
                .getBean("netProxyManager");
        ParamsVo params = new ParamsVo();
        params.setParams("sqlKeyId", "ByObjInfo");
        params.setKey("publicForSql");
        params.setParams("dyncSql", dyncSql);
        log.info("申购统计信息SQL=>" + dyncSql);
        ResultEntity dymicSqlParamResult = (ResultEntity) netProxyManager.doInvoke(params);
        List<DataMap> list = (List) dymicSqlParamResult.getEntity();
        return list;
    }
}
